package com.samuel.blog.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.github.pagehelper.PageInfo;
import com.samuel.blog.bean.PageBean;
import com.samuel.blog.constant.CommonConstant;
import com.samuel.blog.controller.BaseController;
import com.samuel.blog.entity.AuthorEntity;
import com.samuel.blog.entity.CommentEntity;
import com.samuel.blog.service.ArticleService;
import com.samuel.blog.service.AuthorService;
import com.samuel.blog.service.CommentService;
import com.samuel.blog.utils.StringUtils;

/**
 * 控制层 -- 评论系统
 * @author kaiji
 *
 */
@Controller
@RequestMapping("admin")
public class CommentController extends BaseController {

	private static final long serialVersionUID = -2029864014555912188L;
	private static final Logger __logger = LoggerFactory.getLogger(CommentController.class);
	private static final String PRE = "comment/";
	private static final String PRE_PAGER = "admin/comment_";
	
	@Resource(name = "commentService")
	private CommentService commentService; 
	@Resource(name="articleService")
	private ArticleService articleService;
	@Resource(name="authorService")
	private AuthorService authorService;
	
	
	/**
	 * 进入个人评论列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = PRE+"index"+SUFFIX, method = RequestMethod.GET)
	public String index(HttpServletRequest request) {
		this.configCss(request, "comment", "comment_me");
		__logger.info("进入个人评论中心");
		return PRE_PAGER+"me_list";
	}
	
	/**
	 * 获取个人评论列表数据
	 * @param request
	 * @param response
	 * @param pageBean
	 * @return
	 */
	@RequestMapping(value = PRE+"index"+SUFFIX, method = RequestMethod.POST)
	public String index(HttpServletRequest request, HttpServletResponse response, PageBean pageBean) {
		AuthorEntity authorEntity = (AuthorEntity) request.getSession().getAttribute(CommonConstant.USER_LOGIN_SESSION_KEY);
		// 查询自己已经被系统管理员审核通过的评论
		Map<String, Object> authorParam = new HashMap<String, Object>();
		authorParam.put("authorId", authorEntity.getAuthorId());
		authorParam.put("deleteStatus", 0);
		PageInfo<CommentEntity> commentList = commentService.selectPage(pageBean, authorParam);
		// 填充评论人和批评的文章
		List<CommentEntity> list = commentList.getList();
		for (CommentEntity commentEntity : list) {
			if (!StringUtils.empty(commentEntity.getArticleId())) {
				commentEntity.setArticleEntity(articleService.selectById(commentEntity.getArticleId()));
			}
			
			if (!StringUtils.empty(commentEntity.getAuthorId())) {
				commentEntity.setAuthorEntity(authorService.selectById(commentEntity.getAuthorId()));
			}
		}	
		returnTableData(pageBean, commentList, response);
		return null;
	}
	
	
	
	/**
	 * 全部评论列表数据
	 * @return
	 */
	@GetMapping(PRE+"list"+SUFFIX)
	public String list(HttpServletRequest request) {
		this.configCss(request, "comment", "comment_all");
		__logger.info("进入全部评论中心");
		return PRE_PAGER+"all_list";
	}
	
	/**
	 * 全部评论列表数据
	 * @return
	 */
	@PostMapping(PRE+"list"+SUFFIX)
	public String list(HttpServletRequest request, HttpServletResponse response, PageBean pageBean) {
		PageInfo<CommentEntity> commentList = commentService.selectPage(pageBean, null);
		// 填充评论人和批评的文章
		List<CommentEntity> list = commentList.getList();
		if (null != list && list.size() > 0) {
			for (CommentEntity commentEntity : list) {
				commentEntity.setArticleEntity(articleService.selectById(commentEntity.getArticleId()));
				commentEntity.setAuthorEntity(authorService.selectById(commentEntity.getAuthorId()));
			}
		}
		returnTableData(pageBean, commentList, response);
		return null;
	}
	
	/**
	 * 审核 
	 * @return
	 */
	@PostMapping(PRE+"check"+SUFFIX)
	public String check(HttpServletRequest request, HttpServletResponse response, CommentEntity commentEntity) {
		AuthorEntity author = (AuthorEntity)request.getSession().getAttribute(CommonConstant.USER_LOGIN_SESSION_KEY);
		__logger.info("接收到的信息: {}", commentEntity);
		__logger.info("接收到的信息: {}", author);
		
		return null;
	}
}
